package br.cin.ufpe.corba.iiop;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Request {

	private RequestHeader header;
	private RequestBody body;
	
	public Request(RequestHeader header, RequestBody body) {
		this.header = header;
		this.body = body;
	}
	
	public Request(String objectName, String methodName, Object[] arguments, String[] types, boolean hasReturn) {
		header = new RequestHeader();
		header.setObjectKey(objectName);
		header.setOperation(methodName);
		header.setResponseExpected(hasReturn);
		header.setTypes(types);
		
		body = new RequestBody();
		body.setArguments(arguments);
	}
	
	public RequestHeader getHeader() {
		return header;
	}

	public void setHeader(RequestHeader header) {
		this.header = header;
	}

	public RequestBody getBody() {
		return body;
	}

	public void setBody(RequestBody body) {
		this.body = body;
	}

	public void write(OutputStream out) throws IOException {		
		header.write(out);
		body.write(out);
	}
	
	public static Request read(InputStream in) throws IOException, ClassNotFoundException {
		RequestHeader header = RequestHeader.read(in); 
		RequestBody body = RequestBody.read(in);
		return new Request(header, body);
	}
}
